const wx2my = require('../../wx2my');

// pages/pay_order/pay_order.js
const app = getApp();
let page = 1;
let totalpage;
Page({
  data: {
    mengShow: true,
    //蒙层的显示与否
    memberShow: true,
    aniStyle: true,
    //动画效果，默认slideup  
    nodiscount: true,
    discount: true,
    trade_id: '',
    choose: false,
    paytype: 1,
    onOff: 0,
    id: ''
  },
  onLoad: function (e) {
    if (e.trade_id) {
      this.setData({
        trade_id: e.trade_id
      });
    }
  },
  onReady: function () {
    page = 1;
    this.get_list();
    this.get_detail();
  },
  onShow: function () {},

  get_detail() {
    let that = this;
    app.wxRequest('trade_info', {
      'token': wx2my.getStorageSync('token'),
      'trade_id': that.data.trade_id
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        that.setData({
          msg: res.data.data,
          payMoney: res.data.data.neet_money,
          allMoney: res.data.data.neet_money
        });
      }
    });
  },

  get_list() {
    let that = this;
    app.wxRequest('user_coupon', {
      'token': wx2my.getStorageSync('token'),
      'trade_id': that.data.trade_id,
      'page': page
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        let data = res.data.data;
        let list = res.data.data.list;

        if (page == 1) {
          totalpage = res.data.data.totalpage;
        }

        that.setData({
          data: data,
          list: page == 1 ? list : that.data.list.concat(list),
          lock: totalpage <= page ? true : false
        });
      }
    });
  },

  chooseCoupon: function (e) {
    let that = this; //记录上次点击的对象的序号

    let oldidx = that.data.id; //记录当前点击的对象的序号

    let price = e.currentTarget.dataset.money;
    let id = e.currentTarget.dataset.id;

    if (oldidx == id) {
      let select = that.data.select;
      that.setData({
        id: '',
        payMoney: that.data.allMoney,
        couponMoney: '',
        select: !select
      });
    } else {
      that.setData({
        id: id,
        select: true,
        couponMoney: "-￥" + price,
        payMoney: (that.data.payMoney - price).toFixed(2) <= 0 ? 0 : (that.data.payMoney - price).toFixed(2)
      });
      console.log(that.data.id);
    }
  },
  coupons: function (e) {
    this.setData({
      mengShow: false,
      aniStyle: true
    });
  },

  display() {
    this.setData({
      mengShow: true,
      aniStyle: false
    });
  },

  radiochange(e) {
    console.log(e.detail.value);
    let that = this;
    that.setData({
      paytype: e.detail.value
    });
  },

  topay() {
    if (this.data.onOff) return false;
    this.setData({
      onOff: 1
    });
    let that = this;
    app.wxRequest('pay_trade', {
      'token': wx2my.getStorageSync('token'),
      'trade_id': that.data.trade_id,
      'coupon_id': that.data.id,
      'type': that.data.paytype
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        let trade_id = res.data.data.trade_id;
        let data = res.data.data.payparams.success;
        let payParams = res.data.data;
 
        my.tradePay({
          // 'timeStamp': data.timeStamp,
          // 'nonceStr': data.nonceStr,
          // 'package': data.package,
          // 'signType': 'MD5',
          // 'paySign': data.paySign,
          tradeNO: payParams.tradeNO,
          'success': function (res) {
            console.log(res);
            that.setData({
              success: false
            });
            wx2my.reLaunch({
              url: '/pages/home/home?trade_id=' + trade_id
            });
          },
          'fail': function (err) {
            that.setData({
              onOff: 0
            });
            console.log(err);
          }
        });
      } else if (res.data.code == 100) {
        let trade_id = res.data.data.trade_id;
        wx2my.reLaunch({
          url: '/pages/home/home?trade_id=' + trade_id
        });
      } else {
        that.setData({
          onOff: 0
        });
      }
    });
  },

  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    page++;

    if (!this.data.lock) {
      this.get_list();
    }
  },

  couponScroll() {
    let that = this;
    page++;

    if (!that.data.lock) {
      that.get_list();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});